package top.lishuoboy.hibernate.validator.model;

import cn.hutool.core.lang.RegexPool;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import org.hibernate.validator.constraints.CodePointLength;
import org.hibernate.validator.constraints.Range;
import top.lishuoboy.hibernate.validator.dict.Gender;
import top.lishuoboy.hibernate.validator.validate.group.DeleteById;
import top.lishuoboy.hibernate.validator.validate.group.Insert;
import top.lishuoboy.hibernate.validator.validate.group.SelectById;
import top.lishuoboy.hibernate.validator.validate.group.UpdateById;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * 用户
 *
 * @TableName sys_user
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Accessors(chain = true)
@ApiModel(description = "用户")
@TableName(value = "sys_user")
public class SysUser implements Serializable {

    /** 用户id */
    @NotNull(groups = {DeleteById.class, UpdateById.class, SelectById.class})
    @Null(groups = {Insert.class})
    @Min(0)
    @ApiModelProperty(value = "用户id", position = 10)
    @TableId(value = "user_id", type = IdType.AUTO)
    private Integer userId;

    /** 部门id */
    @NotNull(groups = {Insert.class})
    @Min(0)
    @ApiModelProperty(value = "部门id", position = 20)
    @TableField(value = "dept_id")
    private Integer deptId;

    /** 姓名 */
    @NotBlank(groups = {Insert.class})
    @Size(min = 2, max = 100)
    @Pattern(regexp = RegexPool.CHINESE_NAME)
    @ApiModelProperty(value = "姓名", position = 30)
    @TableField(value = "user_name")
    private String userName;

    /** 邮箱 */
    @CodePointLength(max = 100)
//    @Pattern(regexp = RegexPool.EMAIL)
    @Email
    @ApiModelProperty(value = "邮箱", position = 40)
    @TableField(value = "email")
    private String email;

    /** 性别。java enum类型，自动校验，默认支持knife4j的请求参数下拉框（非@RequestBody时） */
    @NotNull(groups = {Insert.class})
//    @Pattern(regexp = "[fm]")
    @ApiModelProperty(value = "性别", position = 50)
    @TableField(value = "gender")
    private Gender gender;

    /** 婚否。java String类型，需用@Pattern校验，需设置allowableValues属性支持knife4j的请求参数支持下拉框（非@RequestBody时） */
    @NotBlank(groups = {Insert.class})
    @Pattern(regexp = "[yn]")
    @ApiModelProperty(value = "婚否", allowableValues = "y,n", position = 60)
    private String marry;

    /** 生日 */
    @NotNull(groups = {Insert.class})
    @PastOrPresent
    @ApiModelProperty(value = "生日", position = 70)
    @TableField(value = "birthday")
    private LocalDate birthday;

    /** 月薪 */
    @Digits(integer = 8, fraction = 2)
    @DecimalMin("0.1")
    @Range(min = 0)
    @ApiModelProperty(value = "月薪", position = 80)
    @TableField(value = "salary")
    private BigDecimal salary;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}